home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue46 / ComCorn / UiMain.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-05-02  |  5.0 KB  |  214 lines

  1. unit UiMain;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   Buttons, ExtCtrls, Menus, TTTServer_TLB, ComCtrls;
  8.  
  9. type
  10.   TRecord = record
  11.     Wins, Loses, Ties: Integer;
  12.   end;
  13.  
  14.   TFrmMain = class(TForm)
  15.     SbTL: TSpeedButton;
  16.     SbTM: TSpeedButton;
  17.     SbTR: TSpeedButton;
  18.     SbMM: TSpeedButton;
  19.     SbBL: TSpeedButton;
  20.     SbBR: TSpeedButton;
  21.     SbMR: TSpeedButton;
  22.     SbBM: TSpeedButton;
  23.     SbML: TSpeedButton;
  24.     Bevel1: TBevel;
  25.     Bevel2: TBevel;
  26.     Bevel3: TBevel;
  27.     Bevel4: TBevel;
  28.     MainMenu1: TMainMenu;
  29.     FileItem: TMenuItem;
  30.     HelpItem: TMenuItem;
  31.     ExitItem: TMenuItem;
  32.     AboutItem: TMenuItem;
  33.     SkillItem: TMenuItem;
  34.     UnconItem: TMenuItem;
  35.     AwakeItem: TMenuItem;
  36.     NewGameItem: TMenuItem;
  37.     N1: TMenuItem;
  38.     StatusBar: TStatusBar;
  39.     procedure FormCreate(Sender: TObject);
  40.     procedure ExitItemClick(Sender: TObject);
  41.     procedure SkillItemClick(Sender: TObject);
  42.     procedure AboutItemClick(Sender: TObject);
  43.     procedure SBClick(Sender: TObject);
  44.     procedure NewGameItemClick(Sender: TObject);
  45.   private
  46.     FXImage: TBitmap;
  47.     FOImage: TBitmap;
  48.     FCurrentSkill: Integer;
  49.     FGameID: Integer;
  50.     FGameServer: IGameServer;
  51.     FRec: TRecord;
  52.     procedure TagToCoord(ATag: Integer; var Coords: TPoint);
  53.     function CoordToCtl(const Coords: TPoint): TSpeedButton;
  54.     procedure DoGameResult(GameRez: GameResults);
  55.   end;
  56.  
  57. var
  58.   FrmMain: TFrmMain;
  59.  
  60. implementation
  61.  
  62. uses UiAbout;
  63.  
  64. {$R *.DFM}
  65.  
  66. {$R xo.res}
  67.  
  68. const
  69.   RecStr = 'Wins: %d, Loses: %d, Ties: %d';
  70.  
  71. procedure TFrmMain.FormCreate(Sender: TObject);
  72. begin
  73.   // load "X" and "O" images from resource into TBitmaps
  74.   FXImage := TBitmap.Create;
  75.   FXImage.LoadFromResourceName(MainInstance, 'x_img');
  76.   FOImage := TBitmap.Create;
  77.   FOImage.LoadFromResourceName(MainInstance, 'o_img');
  78.   // set default skill
  79.   FCurrentSkill := slAwake;
  80.   // init record UI
  81.   with FRec do
  82.    StatusBar.SimpleText := Format(RecStr, [Wins, Loses, Ties]);
  83.   // Get server instance
  84.   FGameServer := CoGameServer.Create;
  85.   // Start a new game
  86.   FGameServer.NewGame(FGameID);
  87. end;
  88.  
  89. procedure TFrmMain.ExitItemClick(Sender: TObject);
  90. begin
  91.   Close;
  92. end;
  93.  
  94. procedure TFrmMain.SkillItemClick(Sender: TObject);
  95. begin
  96.   with Sender as TMenuItem do
  97.   begin
  98.     Checked := True;
  99.     FCurrentSkill := Tag;
  100.   end;
  101. end;
  102.  
  103. procedure TFrmMain.AboutItemClick(Sender: TObject);
  104. begin
  105.   // Show About box
  106.   with TFrmAbout.Create(Application) do
  107.     try
  108.       ShowModal;
  109.     finally
  110.       Free;
  111.     end;
  112. end;
  113.  
  114. procedure TFrmMain.TagToCoord(ATag: Integer; var Coords: TPoint);
  115. begin
  116.   case ATag of
  117.     0: Coords := Point(1, 1);
  118.     1: Coords := Point(1, 2);
  119.     2: Coords := Point(1, 3);
  120.     3: Coords := Point(2, 1);
  121.     4: Coords := Point(2, 2);
  122.     5: Coords := Point(2, 3);
  123.     6: Coords := Point(3, 1);
  124.     7: Coords := Point(3, 2);
  125.   else
  126.     Coords := Point(3, 3);
  127.   end;
  128. end;
  129.  
  130. function TFrmMain.CoordToCtl(const Coords: TPoint): TSpeedButton;
  131. begin
  132.   Result := nil;
  133.   with Coords do
  134.     case X of
  135.       1:
  136.         case Y of
  137.           1: Result := SbTL;
  138.           2: Result := SbTM;
  139.           3: Result := SbTR;
  140.         end;
  141.       2:
  142.         case Y of
  143.           1: Result := SbML;
  144.           2: Result := SbMM;
  145.           3: Result := SbMR;
  146.         end;
  147.       3:
  148.         case Y of
  149.           1: Result := SbBL;
  150.           2: Result := SbBM;
  151.           3: Result := SbBR;
  152.         end;
  153.     end;
  154. end;
  155.  
  156. procedure TFrmMain.SBClick(Sender: TObject);
  157. var
  158.   Coords: TPoint;
  159.   GameRez: GameResults;
  160.   SB: TSpeedButton;
  161. begin
  162.   if Sender is TSpeedButton then
  163.   begin
  164.     SB := TSpeedButton(Sender);
  165.     if SB.Glyph.Empty then
  166.     begin
  167.       with SB do
  168.       begin
  169.         TagToCoord(Tag, Coords);
  170.         FGameServer.PlayerMove(FGameID, Coords.X, Coords.Y, GameRez);
  171.         Glyph.Assign(FXImage);
  172.       end;
  173.       if GameRez = grInProgress then
  174.       begin
  175.         FGameServer.ComputerMove(FGameID, FCurrentSkill, Coords.X, Coords.Y, GameRez);
  176.         CoordToCtl(Coords).Glyph.Assign(FOImage);
  177.       end;
  178.       DoGameResult(GameRez);
  179.     end;
  180.   end;
  181. end;
  182.  
  183. procedure TFrmMain.NewGameItemClick(Sender: TObject);
  184. var
  185.   I: Integer;
  186. begin
  187.   FGameServer.NewGame(FGameID);
  188.   for I := 0 to ControlCount - 1 do
  189.     if Controls[I] is TSpeedButton then
  190.       TSpeedButton(Controls[I]).Glyph := nil;
  191. end;
  192.  
  193. procedure TFrmMain.DoGameResult(GameRez: GameResults);
  194. const
  195.   EndMsg: array[grTie..grComputerWin] of string = (
  196.     'Tie game', 'You win', 'Computer wins');
  197. begin
  198.   if GameRez <> grInProgress then
  199.   begin
  200.     case GameRez of
  201.       grComputerWin: Inc(FRec.Loses);
  202.       grPlayerWin: Inc(FRec.Wins);
  203.       grTie: Inc(FRec.Ties);
  204.     end;
  205.     with FRec do
  206.      StatusBar.SimpleText := Format(RecStr, [Wins, Loses, Ties]);
  207.     if MessageDlg(Format('%s! Play again?', [EndMsg[GameRez]]), mtConfirmation,
  208.       [mbYes, mbNo], 0) = mrYes then
  209.       NewGameItemClick(nil);
  210.   end;
  211. end;
  212.  
  213. end.
  214.